<?php

/**
 * This is the model class for table "equation".
 *
 * The followings are the available columns in table 'equation':
 * @property string $id
 * @property integer $first
 * @property integer $operator
 * @property integer $second
 * @property string $answer
 * @property integer $left
 * @property integer $tof
 * @property string $answer_date
 * @property string $uid
 */
class Equation extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Equation the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{equation}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('first, operator, second, answer, left, ', 'required'),
			array('first, operator, second, left, tof', 'numerical', 'integerOnly'=>true),
			array('answer, uid', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, first, operator, second, answer, left, tof, answer_date, uid', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'first' => 'First',
			'operator' => 'Operator',
			'second' => 'Second',
			'answer' => 'Answer',
			'left' => 'Left',
			'tof' => 'Tof',
			'answer_date' => 'Answer Date',
			'uid' => 'Uid',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('first',$this->first);
		$criteria->compare('operator',$this->operator);
		$criteria->compare('second',$this->second);
		$criteria->compare('answer',$this->answer,true);
		$criteria->compare('left',$this->left);
		$criteria->compare('tof',$this->tof);
		$criteria->compare('answer_date',$this->answer_date,true);
		$criteria->compare('uid',$this->uid,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 
	 * get how many queston use did today.
	 */
	public function getTodayQuestionNum()
	{
	    $criteria = new CDbCriteria();
	    
	    $todayDate = date('Y-m-d');
	    $todayNum = date('d')+1;
	    $tomorrowDate = date('Y-m-').$todayNum;
	    $criteria->condition = ' answer_date > "'.$todayDate.'" and answer_date< "'.$tomorrowDate.'"';
	    $criteria->condition .= ' and uid = '.Yii::app()->user->id;
	    
	    return Equation::model()->count($criteria);
	}
	
	public function getTodayRightNum()
	{
	    $criteria = new CDbCriteria();
	    
	    $todayDate = date('Y-m-d');
	    $todayNum = date('d')+1;
	    $tomorrowDate = date('Y-m-').$todayNum;
	    $criteria->condition = ' answer_date > "'.$todayDate.'" and answer_date< "'.$tomorrowDate.'"';
	    $criteria->condition .= ' and uid = '.Yii::app()->user->id;
	    $criteria->condition .= ' and tof = 1 ';
	    
	    return Equation::model()->count($criteria);
	}
	
	public function getTodayQuestionTypeDetail()
	{
	    $todayDate = date('Y-m-d');
	    $todayNum = date('d')+1;
	    $tomorrowDate = date('Y-m-').$todayNum;
	    
	    $sql = "SELECT tof, map_type_code, count(map_type_code) as count_time FROM `{{equation}}` ";
	    $sql.= " where map_type_code is not null ";
	    $sql.= " and uid=".Yii::app()->user->id;
	    $sql.= " and (answer_date >'".$todayDate."' and answer_date < '".$tomorrowDate."' ) ";
	    $sql.= " group by map_type_code, tof ";
	    
	    $countCommand = Yii::app()->db->createCommand($sql);
	    $result = $countCommand->query();
	    return $result;
	}
	
	/**
	 * 
	 * Generate Math Equation use code in database as identity 
	 * @param unknown_type $reType
	 */
	public function generateMath($reType, $offSetRe=0)
	{
	      $PLUS_UNDER_20='L001';
        $PLUS_UNDER_100='L002';
        $MINUS_UNDER_20='L005';
        $MINUS_UNDER_100='L006';
        $PORM_UNDER_20='L009';
        $PORM_UNDER_100='L010';
        
        $MULTI_1_BIT='L011';
        $MULTI_2_BITS='L012';
        $DIVISION_1_BIT='L021';
        $DIVISION_2_BITS='L022';
        
        $MORD_1_BIT='L031';
        $MORD_2_BITS='L032';
        
        $ALL_TYPE_2_BITS='L051';
        $ALL_TYPE_4_BITS='L052';
        
        $REVIEW_TODAY = 'L061';
        $REVIEW_ALL = 'L062';
        
	      $EF = 0;
        $ES = 0;
        $EO = 0;
        $id = 0; //the id for reequation record, when new equation show ZERO
        
        //give a default value
        if (empty($reType))
            $reType= 'L001';
        
//        if (isset($_GET['re_type'])){
//            $reType = $_GET['re_type'];
//        }
        
        switch ($reType) {
            case $PLUS_UNDER_20:
            $EO = 1;
            $EF = rand(0, 20);
            $ES = rand(0, 20-$EF);
            break;
            
            case $PLUS_UNDER_100:
            $EO = 1;
            $EF = rand(0, 100);
            $ES = rand(0, 100-$EF);
            break;
            
            case $MINUS_UNDER_20:
            $EO = 2;
            $EF = rand(0, 20);
            $ES = rand(0, $EF);
            break;
            
            case $MINUS_UNDER_100:
            $EO = 2;
            $EF = rand(0, 100);
            $ES = rand(0, $EF);
            break;
            
            case $PORM_UNDER_20:
            $EO = rand(1, 2);
            $EF = rand(0, 20);
            if ($EO == 1)
            $ES = rand(0, 20-$EF);
            else 
            $ES = rand(0, $EF);
            break;
            
            case $PORM_UNDER_100:
            $EO = rand(1, 2);
            $EF = rand(0, 100);
            if ($EO == 1)
            $ES = rand(0, 100-$EF);
            else 
            $ES = rand(0, $EF);
            break;
            
            case $MULTI_1_BIT:
            $EO = 3;
            $EF = rand(0, 9);
            $ES = rand(0, 9);
            break;
            
            case $MULTI_2_BITS:
            $EO = 3;
            $EF = rand(10, 99);
            $ES = rand(10, 99);
            break;
            
            case $DIVISION_1_BIT:
            $EO = 4;
            $EF = rand(0, 9);
            $ES = rand(0, $EF);
            break;
            
            case $DIVISION_2_BITS:
            $EO = 4;
            $EF = rand(10, 99);
            $ES = rand(0, $EF);
            break;
            
            case $MORD_1_BIT:
            $EO = rand(3, 4);
            $EF = rand(0, 9);
            $ES = rand(0, 9);
            if ($EO == 4)
                $ES = rand(0, $EF);
            break;
            
            case $MORD_2_BITS:
            $EO = rand(3, 4);
            $EF = rand(10, 99);
            $ES = rand(10, 99);
            if ($EO == 4)
                $ES = rand(0, $EF);
            break;
            
            case $ALL_TYPE_2_BITS:
            $EO = rand(1, 4);
            $EF = rand(0, 99);
            $ES = rand(0, 99);
            if ($EO == 2 || $EO == 4)
                $ES = rand(0, $EF);
            break;
            
            case $ALL_TYPE_4_BITS:
            $EO = rand(1, 4);
            $EF = rand(100, 9999);
            $ES = rand(100, 9999);
            if ($EO == 2 || $EO == 4)
                $ES = rand(0, $EF);
            break;
            
            case $REVIEW_TODAY:
                $reequation  = Reequation::model()->getUserReequation(1, $offSetRe);
                $EF = $reequation['first'];
                $ES = $reequation['second'];
                $EO = $reequation['operator'];
                $id = $reequation['id'];
                break;
            
                
            case $REVIEW_ALL:
                $reequation  = Reequation::model()->getUserReequation(0, $offSetRe);
                $EF = $reequation['first'];
                $ES = $reequation['second'];
                $EO = $reequation['operator'];
                $id = $reequation['id'];
                break;
            
            default:
            $EO = 1;
            break;
        }
        
        $arrMathElements =array('F'=>$EF , 'S'=>$ES, 'O'=>$EO, 'id'=>$id);
        return $arrMathElements;
	}
	

	/**
	 * 
	 * give all elements, return right answer.
	 * @param unknown_type $f
	 * @param unknown_type $op
	 * @param unknown_type $s
	 */
	public function getEquationAnswer()
	{
	    $f = (int)$this->first;
	    $s = (int)$this->second;
	    $o = (int)$this->operator;
	    
//	    var_dump($s);
	    
	    if ($o === 4 &&($f === 0||$s === 0)){
	        return array('A'=>0, 'L'=>0);
	    }else
	    switch ($this->operator) {
	        case 1:
	        return array('A'=>$f+$s, 'L'=>0);
	        break;
	        
	        case 2:
	        return array('A'=>$f-$s, 'L'=>0);
	        break;
	        
	        case 3:
	        return array('A'=>$f*$s, 'L'=>0);
	        break;
	        
	        case 4:
	        return array('A'=>floor($f/$s), 'L'=>$f%$s);
	        break;
	        
	        default:
	        return array('A'=>0, 'L'=>0);
	        break;
	    }
	}
	
	/**
	 * set add date and user id, before save operate.
	 * @see CActiveRecord::beforeSave()
	 */
	public function beforeSave()
	{
	    if (parent::beforeSave()){
	        if ($this->isNewRecord){
	            $this->answer_date = date('Y-m-d H:i:s');
	            $this->uid = Yii::app()->user->id;
 	        }
	        return true;
	    }else{
	        return false;
	    }
	}
	
}